package patrol.mobile.com.mobilepatrol;

import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.test.GsonTest;

public class ScanActivity extends BaseActivity implements QRCodeView.Delegate {
    private static final String TAG = ScanActivity.class.getSimpleName();

    private QRCodeView mQRCodeView;

    @Override
    protected void onStart() {
        super.onStart();
        // 打开后置摄像头开始预览，但是并未开始识别
        mQRCodeView.startCamera();
        //显示扫描框
        mQRCodeView.showScanRect();
        //开始扫描
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        //关闭摄像头预览，并且隐藏扫描框
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }
    //震动
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        startActivity(new Intent(ScanActivity.this,GsonTest.class));
        Log.i(TAG, "result:" + result);
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        vibrate();
        mQRCodeView.startSpot();

    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(ScanActivity.this,"打开相机出错", Toast.LENGTH_SHORT).show();
        Log.e(TAG, "打开相机出错");
        finish();
    }
    @Override
    public void initTitle() {
//        ImageView iv_back = ((ImageView) findViewById(R.id.iv_back));
//        iv_back.setImageResource(R.mipmap.up);
//        iv_back.setVisibility(View.VISIBLE);
//        iv_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Utils.hiddenKeyBoard(ScanActivity.this);
//                Utils.exitActivityAndBackAnim(ScanActivity.this,true);
//            }
//        });
//        TextView tv_title = ((TextView) findViewById(R.id.tv_center_title));
//        tv_title.setVisibility(View.VISIBLE);
//        tv_title.setText("扫描二维码");
    }

    @Override
    public void initView() {
        super.initView();
        mQRCodeView = (ZXingView) findViewById(R.id.zxingview);
        //设置扫描二维码的代理
        mQRCodeView.setDelegate(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_scan;
    }

    @Override
    public void setListener() {

    }
}